'''
Created on 2020年3月3日

@author: Administrator
'''
import time, datetime

from dateutil.relativedelta import relativedelta

from common.public import *
from orm.model import User, Notify, TrackRecord, MyCustomer, Customer, SummaryTrack, db
from view.baseview import BaseView
from view.mycustomerview import MyCustomerView


class TrackRecordView(BaseView):
    def __init__(self):
        pass

    @db.atomic()                   
    def addTrackRecord(self, params):
        """
        添加
        """
        for id in params["ids"]:
            TrackRecord.create(
                mycustomer_id = id,
                tracktype = params["tracktype"],
                createtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                content = params["content"]
            )
#             print(id)
            whereis = (MyCustomer.id == id)
            content_str = ""
            type_str = ""
            status = MYCUSTOMER_STATUS_TRACKED
            if params["tracktype"] == TRACK_DIANHUAN or params["tracktype"] == TRACK_QITA:
                status  = MYCUSTOMER_STATUS_TRACKED
            elif params["tracktype"] == TRACK_MIANFANG:
                status  = MYCUSTOMER_STATUS_VISITED
            elif params["tracktype"] == TRACK_QIANYUE:
                status  = MYCUSTOMER_STATUS_SIGNED
                # 判定是否为客户签约
                content_str = "客户：" + params["compname"] +"状态：已签约",
                type_str = NOTIFY_TYPE_CUSTOMER_DEAL
            else:
                status  = MYCUSTOMER_STATUS_TRACKED
            
            # 更新过期日期
            mc = MyCustomer.get(whereis)
            curtrackerid = mc.tracker_id
            if mc.saletype==SALE_TYPE_NONE:
                # 无类别客户三天期限
                enddatetime_str = (datetime.datetime.now() +datetime.timedelta(days=SALE_TYPE_1DAY)).strftime("%Y-%m-%d %H:%M:%S")
            elif mc.saletype==SALE_TYPE_A:
                # A类别客户7天期限
                enddatetime_str = (datetime.datetime.now()+datetime.timedelta(days=SALE_TYPE_7DAY)).strftime("%Y-%m-%d %H:%M:%S")
            elif mc.saletype==SALE_TYPE_B:
                # B类别客户14天期限
                enddatetime_str = (datetime.datetime.now()+datetime.timedelta(days=SALE_TYPE_15DAY)).strftime("%Y-%m-%d %H:%M:%S")
            elif mc.saletype==SALE_TYPE_C:
                # C类别客户30天期限
                enddatetime_str = (datetime.datetime.now()+datetime.timedelta(days=SALE_TYPE_30DAY)).strftime("%Y-%m-%d %H:%M:%S")
            else:
                # 无类别客户三天期限
                enddatetime_str = (datetime.datetime.now()+datetime.timedelta(days=SALE_TYPE_1DAY)).strftime("%Y-%m-%d %H:%M:%S")

            mc.status = status
            mc.updatetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            mc.enddatetime = enddatetime_str
            mc.memo = params["content"]
            mc.save()

            if type_str!="":
                #找到部门主管发送通知
                user = User.get(User.id==curtrackerid)
                print(user)
                Notify.create(
                    notifier_id = user.leader_id,
                    createdatetime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                    type = type_str,
                    content = content_str,
                    status = NOTIFY_STATUS_UDPRESSED,
                    tracker_id = curtrackerid,
                    memo = ""
                )
                
            # 创建统计数据
            mycusview = MyCustomerView()
            mycusview.createSummaryData(curtrackerid)

            # 取得当前日期的跟踪数据
            curdaycondtion = (SummaryTrack.tracker == curtrackerid) & (SummaryTrack.date == time.strftime("%Y-%m-%d", time.localtime()))
            strack = SummaryTrack.get(curdaycondtion)
            strack.total = strack.total + 1
            strack.save()

        return super().resultSet(200, "添加成功", [])
        raise Exception("数据库操作失败")
